package uk.ac.bbk.dcs.muc;

/**
 * Copyright [2011] [Sean Howes and Aruna Gunasekera]
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import static uk.ac.bbk.dcs.muc.EventSqlLiteHelper.*;

public class KmlBuilder {
	private static final String SELECT_FROM_EVENT_TABLE = "SELECT * FROM " + EVENT_TABLE + " WHERE " + EVENT_NAME;
	private static String longEvent = SELECT_FROM_EVENT_TABLE + "='longitude' ORDER BY " + EVENT_TIME;
	private static String latEvent = SELECT_FROM_EVENT_TABLE + "='latitude'  AND " + EVENT_TIME + "=";
	private static String altEvent = SELECT_FROM_EVENT_TABLE + "='altitude'  AND " + EVENT_TIME + "=";
	private static String otherEvents = SELECT_FROM_EVENT_TABLE + "!= 'longitude' AND " + EVENT_NAME + "!='latitude' AND " + EVENT_NAME + "!= 'altitude' AND "
			+ EVENT_TIME + "=";

	public static int counter;

	public static String buildKmlFromDatabase(SQLiteDatabase database) {
		counter = 0;

		StringBuilder builder = new StringBuilder();
		builder.append(documentHeader());

		Cursor row = database.rawQuery(longEvent, null);
		row.moveToFirst();
		while (!row.isAfterLast()) {
			String timestamp = row.getString(TIME_STAMP_COL_NO);
			String longitude = row.getString(DATA_COLUMN_NO);

			Cursor latCur = database.rawQuery(latEvent + timestamp, null);
			Cursor altCur = database.rawQuery(altEvent + timestamp, null);
			String latitude = getDataValueFromCursor(latCur);
			String altitude = getDataValueFromCursor(altCur);

			StringBuilder detailBuilder = getPlacemarkDetails(database, timestamp);
			builder.append(buildPlaceMark(timestamp, longitude, latitude, altitude, detailBuilder.toString()));
			row.moveToNext();
		}
		row.close();
		builder.append(documentFooter());
		return builder.toString();
	}

	private static StringBuilder getPlacemarkDetails(SQLiteDatabase database, String timestamp) {
		StringBuilder detailBuilder = new StringBuilder();
		Cursor otherRows = database.rawQuery(otherEvents + timestamp, null);
		otherRows.moveToFirst();
		detailBuilder.append("<![CDATA[");
		detailBuilder.append("<ul>");
		while (!otherRows.isAfterLast()) {
			String value = otherRows.getString(DATA_COLUMN_NO);
			if (value != null) {
				detailBuilder.append("<li><b>" + otherRows.getString(NAME_COL_NO) + " : " + value + "</b></li>");
			}
			otherRows.moveToNext();
		}
		detailBuilder.append("</ul>");
		detailBuilder.append("]]>");
		otherRows.close();
		return detailBuilder;
	}

	private static String getDataValueFromCursor(Cursor cursort) {
		String value = "";
		if (cursort.moveToFirst()) {
			value = cursort.getString(DATA_COLUMN_NO);
			cursort.close();
		}
		return value;
	}

	private static String documentHeader() {
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document><name>Life Log</name><open>1</open>";
	}

	private static String documentFooter() {
		return " </Document></kml>";
	}

	private static String buildPlaceMark(String dateTimeString, String longitude, String latitude, String altitude, String details) {
		counter++;
		return "<Placemark><name> Marker " + counter + "</name><description>" + details + "</description><TimeStamp><when>" + dateTimeString
				+ "</when></TimeStamp><Point><coordinates>" + longitude + "," + latitude + "," + altitude + "</coordinates>" + "</Point></Placemark>";
	}

}
